/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xmlreader;

import filetrans.Client;
import java.io.InputStream;
import java.util.Iterator;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import model.Torrent;

/**
 *
 * @author Oscar
 */
public class CommandInterpreter {

    public CommandInterpreter() {

    }

    /**
     * Interpreta un mensaje XML recibido por el servidor
     *
     * @param configFile
     * @return
     */
    @SuppressWarnings({ "unchecked", "null" })
    public void readCommand(InputStream command, Object caller) {

        try {

            // Fabrica de entrada de XML
            XMLInputFactory inputFactory = XMLInputFactory.newInstance();

            // Se crea un nuevo eventReader
            XMLEventReader eventReader = inputFactory.createXMLEventReader(command);

            while (eventReader.hasNext()) {

                XMLEvent event = eventReader.nextEvent();

                if (event.isStartElement()) {

                    StartElement startElement = event.asStartElement();

                    // Si tenemos un comando authenticate
                    if (startElement.getName().getLocalPart().equals("response")) {

                        Client client = (Client) caller;

                        //Como se que la respuesta es string la asigno a
                        //StringResponse
                        client.setStringResponse(CommandResponse(startElement));
                    }

                    // Si tenemos un comando download
                    if (startElement.getName().getLocalPart().equals("download")) {

                        Client client = (Client) caller;

                        //Como se que la respuesta es string la asigno a
                        //StringResponse
                        client.setTorrent(CommandDownload(startElement));
                    }

                }

            }

        }
        catch (XMLStreamException e) {
        }
    }

    /**
     * Llama a la funcion asociada al tag XML
     *
     * @param startElement Tag XML
     */
    public String CommandResponse (StartElement startElement){


        String yesOrNo = null;

        // Se leen los atributos del comando
        Iterator<Attribute> attributes = startElement
                        .getAttributes();

        while (attributes.hasNext()) {

            Attribute attribute = attributes.next();

            if (attribute.getName().toString().equals("value")) {

                yesOrNo = attribute.getValue();
            }

        }

        return yesOrNo;

    }


    public Torrent CommandDownload (StartElement startElement){


        Torrent torrent = new Torrent();

        // Se leen los atributos del comando
        Iterator<Attribute> attributes = startElement
                        .getAttributes();

        while (attributes.hasNext()) {

            Attribute attribute = attributes.next();

            if (attribute.getName().toString().equals("user")) {

                torrent.setUser(attribute.getValue());
            }
            if (attribute.getName().toString().equals("name")) {

                torrent.setName(attribute.getValue());
            }
            if (attribute.getName().toString().equals("block")) {

                torrent.setBlock(attribute.getValue());
            }

        }

        return torrent;

    }


}
